package com.xiaoyudeguang.customizer;

import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.oas.models.media.Schema;
import org.springdoc.core.customizers.PropertyCustomizer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

import javax.persistence.Column;
import java.util.Arrays;

/**
 * @author 赵光
 */
@Component
@ConditionalOnClass(Column.class)
public class CustomPropertyCustomizer implements PropertyCustomizer {

    @Override
    @SuppressWarnings("rawtypes")
    public Schema customize(Schema property, AnnotatedType type) {
        Arrays.stream(type.getCtxAnnotations()).filter(it -> it.annotationType() == Column.class).findFirst().ifPresent(it -> {
            String columnDefinition = ((Column) it).columnDefinition();
            property.setDescription(columnDefinition.substring(columnDefinition.indexOf("'") + 1, columnDefinition.lastIndexOf("'")));
        });
        return property;
    }
}
